﻿using System;
using System.Collections.Generic;
using BitBenderGames;
using UnityEngine;
using Yoozoo.Gameplay;
using Yoozoo.Gameplay.City;
using Yoozoo.Mars.Got;

namespace Yoozoo.Core.Common
{
    [Serializable]
    public class GameObjectGradingData
    {
        public List<GameObject> needHideObjList;
    }

    public class GameObjectGrading : MonoBehaviour
    {
        [Header("品质为Perfect、PC时启用")]
        public GameObjectGradingData level0;
        [Header("品质为High、Middle时启用")]
        public GameObjectGradingData level1;
        [Header("品质为Low、VeryLow时启用")]
        public GameObjectGradingData level2;

        private GameObjectGradingData _currentLevelData;
        private int _currentLevel;
        private Shader shadowShader;

        //private Dictionary<SkinnedMeshRenderer, Material> _materialDic;
        
        private MobileTouchCamera mobileTouchCamera;
        private TouchCameraController touchCameraController;
        private float CamZoomMin, CamZoomMax, CamZoomInit, curCamZoom;
        private bool inited = false;
        private Transform camTrans;
        private int shadowState;
        private bool currentShadowState = true;

        private Material shadowMaterial;
        private SkinnedMeshRenderer shadowMesh = null;
        
        private void AddEventListeners()
        {
            GTAGameStart.eventManager.AddEvent(ClientEvent.OnInitCityCameraEnd, OnInitCityCameraEnd);
        }
        private void RomoveEventListeners()
        {
            GTAGameStart.eventManager.RemoveEvent(ClientEvent.OnInitCityCameraEnd, OnInitCityCameraEnd);
        }

        public void ChangeLevel(int lv)
        {
            GameObjectGradingData pd = level2;
            if (lv == 0) pd = level0;
            else if (lv == 1) pd = level1;

            if (_currentLevelData != null)
            {
                // 还原设置,打开显示，并重新设置影子材质球
                if (_currentLevelData.needHideObjList!=null)
                {
                    for (int i = 0; i < _currentLevelData.needHideObjList.Count; i++)
                    {
                        if (_currentLevelData.needHideObjList[i])
                        {
                            _currentLevelData.needHideObjList[i].SetActive(true);
                        }
                    }
                }
                
            }
            if (shadowMesh != null)
            {
                SetShadow(true);
                currentShadowState = true;
            }
            
            _currentLevelData = pd;
            _currentLevel = lv;
            if (_currentLevelData != null)
            {
                if (_currentLevelData.needHideObjList != null)
                {
                    for (int i = 0; i < _currentLevelData.needHideObjList.Count; i++)
                    {
                        if (_currentLevelData.needHideObjList[i])
                        {
                            _currentLevelData.needHideObjList[i].SetActive(false);
                        }
                    }
                }
            }
            

            if (_currentLevel == 2 &&  shadowMesh != null)
            {
                SetShadow(false);
                currentShadowState = false;
            }
            inited = true;
        }


        private void Awake()
        {
            //记录下所有有影子得物体
            var list = transform.GetComponentsInChildren<SkinnedMeshRenderer>();
            //_materialDic = new Dictionary<SkinnedMeshRenderer, Material>();
            for (int i = 0; i < list.Length; i++)
            {
                var materials = list[i].materials;
                for (int j = 0; j < materials.Length; j++)
                {
                    var material = materials[j];
                    if (material)
                    {
                        if (material.name.Contains("role_citynpc_shadow_ai"))
                        {
                            //_materialDic[list[i]] = material;
                            shadowMesh = list[i];
                            shadowMaterial = new Material(material);
                            break;
                        }
                    }
                }
            }
            if (QualityHelper.GameObjectQualityListener!=null)
            {
                QualityHelper.GameObjectQualityListener.AddGameObjectGrading(this);
            }
            AddEventListeners();
            OnInitCityCameraEnd();
        }

        void Update()
        {
            if (shadowMesh == null) return;
            if (inited && camTrans != null)
            {
                var zoom = camTrans.localPosition.y * 1.414f;
                if (Math.Abs(curCamZoom - zoom) < 0.001f)
                {
                    return;
                }
                curCamZoom = zoom;
                if ((CamZoomMax - CamZoomMin)* 0.6f + CamZoomMin < curCamZoom)
                {
                    SetShadow(false);
                }
                else
                {
                    if (currentShadowState)
                    {
                        SetShadow(true);
                    }
                }
                
            }
        }
        private void OnDestroy()
        {
            if (QualityHelper.GameObjectQualityListener != null)
            {
                QualityHelper.GameObjectQualityListener.RemoveGameObjectGrading(this);
            }

            RomoveEventListeners();
            //_materialDic = null;
        }

        //设置影子的开关变更为移除和添加材质球
        private void SetShadow(bool state)
        {
            if (shadowMesh == null) return;
            int stateInt = state?1:2;
            
            if (shadowState == stateInt)
            {
                return;
            }

            shadowState = stateInt;
            
            //替换材质球
            Material[] mats = shadowMesh.materials;
            List<Material> newMats;
            newMats = new List<Material>();
            int count = 0;
            for (int i = 0; i < mats.Length; i++)
            {
                Material mat = mats[i];
                if (!mat.name.Contains("role_citynpc_shadow_ai"))
                {
                    newMats.Add(mat);
                }
            }
            if (state)
            {
                newMats.Add(shadowMaterial);
            }

            Material[] materials = new Material[newMats.Count];
            for (int i = 0; i < newMats.Count; i++)
            {
                materials[i] = newMats[i];
            }
            shadowMesh.materials = materials;
        }
        
        void OnInitCityCameraEnd()
        {
            mobileTouchCamera = CityManager.Instance.mobileTouchCamera;
            if (mobileTouchCamera)
            {
                touchCameraController = mobileTouchCamera.GetComponent<TouchCameraController>();
                float zoomInit = touchCameraController.GetCameraZoomInit();
                if (!zoomInit.Equals(0))
                {
                    CamZoomMin = mobileTouchCamera.CamZoomMin;
                    CamZoomMax = mobileTouchCamera.CamZoomMax;
                    CamZoomInit = touchCameraController.GetCameraZoomInit();
                    camTrans = mobileTouchCamera.Cam.transform;
                }
            }
           
            
        }
    }
}
